

clear
set more 1
set type double

***************************************
*Notes
********************************************
*1. IV regs
*4 sets of instruments
*i. phi, emp, iota (all)
*ii. emp, iota (all without phi)
*iii. iota (sticky price only)
*iv. phi, emp (flexible price only)
********************************************

forv lag=1/24 {

*1. prepare exchange rate data
*this was put together by Noah in data/exchange_rates
**************************************************************
use ../rawdata/xr_monthly_2020, clear
	gen date  = ym(year, month)
	format date %tm
	keep date cty_name cty_code xr_m
	duplicates drop
	drop if missing(xr_m)
	egen cty_num=group(cty_code)
	tsset cty_num date

*xr_m is euro per destination
*redefine exchange rate so an increase is a depreciation of the destination currency
*gen destination per euro
	gen xE=1/xr_m
	gen dxE=ln(xE/l`lag'.xE)
	
*drop outliers: Venezuela, Sudan, Uzbekastan 
	drop if cty_code=="VE"|cty_code=="SD"|cty_code=="UZ"
	drop if abs(dxE)>1
	keep cty_code date xE dxE
save temp/er_monthly_xE,replace


use ../rawdata/xr_monthly_2020, clear
	gen date  = ym(year, month)
	format date %tm
	keep date cty_name cty_code xr_m
	duplicates drop
	drop if missing(xr_m)
	egen cty_num=group(cty_code)
	

preserve
	keep if cty_code=="US"
	gen euro_dollar=xr_m 
	gen xED=1/xr_m
	keep date euro_dollar xED
	duplicates drop
	save temp/euro_dollar, replace
restore
		
	merge m:1 date using temp/euro_dollar, keep(match) nogen
	tsset cty_num date	
	gen xD=(1/xr_m)*euro_dollar
	gen dxD=ln(xD/l`lag'.xD)
	gen dxED=ln(xED/l`lag'.xED)
	*drop outliers: Venezuela, Sudan, Uzbekastan 
	drop if cty_code=="VE"|cty_code=="SD"|cty_code=="UZ"
	
	merge 1:1 cty_code date using temp/er_monthly_xE, keep(match) nogen
	
	keep cty_code cty_name date xE xD euro_dollar dxD dxE dxED
	
	gen test=xD/xE
	sum test euro_dollar
	
	save temp/er_monthly_xD,replace	



******************************************
*2. construct unit values in export data
******************************************	
use temp/exportdata, clear

	tsset fpd date
	gen lagvalue=l`lag'.value	
	gen duv=ln(uv1/l`lag'.uv1)
	replace duv=ln(uv2/l`lag'.uv2) if missing(duv) 
	
	gen dq=ln(q1/l`lag'.q1)
	replace dq=ln(q2/l`lag'.q2) if missing(dq)
	
	gen pratio=uv1/l`lag'.uv1
	replace pratio=uv2/l`lag'.uv2 if missing(pratio)
		
	drop if missing(duv)
		
	drop if (pratio>3 | pratio<1/3)
	
				
*3. combine data
***********************
	merge m:1 vat cty_code using ../data/currency_vat, keep(match) nogen 
	merge m:1 vat cty_code using ../data/currency_vehicle, keep(match master) nogen	

	merge m:1 cty_code date using temp/er_monthly_xD, keep(match) nogen
	merge m:1 vat using ../data/memp, keep(match master) nogen	
	merge m:1 vat using ../data/mphi_xeu_currency, keep(match) nogen
	

*merge manuf indicators
	merge m:1 vat using ../data/manuf_firm, keep(match master) nogen
	keep if manuf_firm==1

*merge in Rauch index
	gen hs6=substr(cncode,1,6)
	merge m:1 hs6 using ../data/Rauch_hs6_concordance, keep(match master) nogen
	gen dif=con=="n"
	
merge m:1 vat cty_code using ../data/currency_vehicle, keep(match master) nogen	
	drop if vehicle_other==1

*************************************
*4. construct variables
***********************************
gen share_other=1-share_euro
	
*mphi_xeu_prod is noneuro and nonUSD
	gen dxE_share_euro=dxE*share_euro
	gen dxE_share_other=dxE*share_other
	gen dxD_share_usd=dxD*share_usd
	gen dxD_share_other=dxD*share_other
		
	gen lnmemp=ln(memp)
	gen dxE_lnmemp=dxE*lnmemp
	gen dxD_lnmemp=dxD*lnmemp	

			
*dxr interactions
	gen dxE_phi=dxE*mphi_xeu
	gen dxD_phi=dxD*mphi_xeu
	
	gen hs4=substr(cncode,1,4)	
	egen cp=group(hs4 cty_code)
	egen cy=group(year month cty_code)
	egen cpy=group(hs4 cty_code date)
	
*convert duv from euro prices to destination prices
	gen duv_k=duv + dxE	
			
save temp/temp_erpt_m`lag', replace


}


******************************************
*ESTIMATION
**************************************************

*1. IV regs
*******************************

*i all instruments
********************************

forv lag=1/24 {
use temp/temp_erpt_m`lag', clear
	eststo: ivreghdfe dq (duv_k = dxE_phi dxD_phi dxD_lnmemp dxE_lnmemp dxE_share_other dxD_share_usd), cluster(cy) absorb(cpy vat)
}


esttab using "../results/dynamic/dynamicQ_cpy_IV1.csv", se b(3) se(3) r2 ar2  plain nostar compress replace
eststo clear	


*ii. without empl
********************************

forv lag=1/24 {
use temp/temp_erpt_m`lag', clear
	eststo: ivreghdfe dq (duv_k = dxE_phi dxD_phi dxE_share_other dxD_share_usd), cluster(cy) absorb(cpy vat)
}


esttab using "../results/dynamic/dynamicQ_cpy_IV2.csv", se b(3) se(3) r2 ar2  plain nostar compress replace
eststo clear	

*iii. sticky price instruments
**********************************
forv lag=1/24 {
use temp/temp_erpt_m`lag', clear
	eststo: ivreghdfe dq (duv_k =  dxE_share_other dxD_share_usd), cluster(cy) absorb(cpy vat)

}

esttab using "../results/dynamic/dynamicQ_cpy_IV3.csv", se b(3) se(3) r2 ar2  plain nostar compress replace
eststo clear	


*************************************
*iv. flexible price instruments

forv lag=1/24 {
use temp/temp_erpt_m`lag', clear
	eststo: ivreghdfe dq (duv_k = dxE_phi dxD_phi dxD_lnmemp dxE_lnmemp), cluster(cy) absorb(cpy vat)
}


esttab using "../results/dynamic/dynamicQ_cpy_IV4.csv", se b(3) se(3) r2 ar2  plain nostar compress replace
eststo clear	

**********************************************************


****************************************************************
*2. FIRST STAGE
**************************************************************

*i all instruments
********************************
forv lag=1/24 {
use temp/temp_erpt_m`lag', clear
	eststo: reghdfe duv_k dxE_phi dxD_phi dxD_lnmemp dxE_lnmemp dxE_share_other dxD_share_usd, cluster(cy) absorb(cpy vat)
}


esttab using "../results/dynamic/dynamicQ_cpy_IV1_fs.csv", se b(3) se(3) r2 ar2  plain nostar compress replace
eststo clear	


*ii. without empl
********************************

forv lag=1/24 {
use temp/temp_erpt_m`lag', clear
	eststo: reghdfe duv_k dxE_phi dxD_phi dxE_share_other dxD_share_usd, cluster(cy) absorb(cpy vat)
}


esttab using "../results/dynamic/dynamicQ_cpy_IV2_fs.csv", se b(3) se(3) r2 ar2  plain nostar compress replace
eststo clear	

*iii. sticky price instruments
**********************************
forv lag=1/24 {
use temp/temp_erpt_m`lag', clear
	eststo: reghdfe duv_k dxE_share_other dxD_share_usd, cluster(cy) absorb(cpy vat)
}

esttab using "../results/dynamic/dynamicQ_cpy_IV3_fs.csv", se b(3) se(3) r2 ar2  plain nostar compress replace
eststo clear	


*************************************
*iv. flexible price instruments

forv lag=1/24 {
use temp/temp_erpt_m`lag', clear
	eststo: reghdfe duv_k dxE_phi dxD_phi dxD_lnmemp dxE_lnmemp, cluster(cy) absorb(cpy vat)
}

esttab using "../results/dynamic/dynamicQ_cpy_IV4_fs.csv", se b(3) se(3) r2 ar2  plain nostar compress replace
eststo clear	


***********************************************************
*3.REDUCED FORM
************************************************************
*i all instruments
********************************

forv lag=1/24 {
use temp/temp_erpt_m`lag', clear
	eststo: reghdfe dq dxE_phi dxD_phi dxD_lnmemp dxE_lnmemp dxE_share_other dxD_share_usd, cluster(cy) absorb(cpy vat)
}


esttab using "../results/dynamic/dynamicQ_cpy_IV1_rf.csv", se b(3) se(3) r2 ar2  plain nostar compress replace
eststo clear	


*ii. without empl
********************************

forv lag=1/24 {
use temp/temp_erpt_m`lag', clear
	eststo: reghdfe dq dxE_phi dxD_phi dxE_share_other dxD_share_usd, cluster(cy) absorb(cpy vat)
}


esttab using "../results/dynamic/dynamicQ_cpy_IV2_rf.csv", se b(3) se(3) r2 ar2  plain nostar compress replace
eststo clear	

*iii. sticky price instruments
**********************************
forv lag=1/24 {
use temp/temp_erpt_m`lag', clear
	eststo: reghdfe dq dxE_share_other dxD_share_usd, cluster(cy) absorb(cpy vat)
}

esttab using "../results/dynamic/dynamicQ_cpy_IV3_rf.csv", se b(3) se(3) r2 ar2  plain nostar compress replace
eststo clear	


*************************************
*iv. flexible price instruments

forv lag=1/24 {
use temp/temp_erpt_m`lag', clear
	eststo: reghdfe dq dxE_phi dxD_phi dxD_lnmemp dxE_lnmemp, cluster(cy) absorb(cpy vat)
}


esttab using "../results/dynamic/dynamicQ_cpy_IV4_rf.csv", se b(3) se(3) r2 ar2  plain nostar compress replace
eststo clear	


